package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.nativec.tools.ModuleManager;
import com.txsj.scan.MyApplication;
import com.txsj.scan.R;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.CodeBean;
import com.txsj.scan.presenter.LoginPresenter;
import com.txsj.scan.view.ILoginView;
import com.txsj.scan.widget.TimeButton;

/**
 * Created by wangchuangjun on 2019/2/18
 * Desc: 登录页面
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginView, View.OnClickListener {


    private EditText et_phone, et_code;
    private Button btn_login;
    private TimeButton timeButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
    }

    private void initData() {
        timeButton.onCreate(null);
    }

    private void initView() {
        mTitle =  findViewById(R.id.mTitle);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_code = (EditText) findViewById(R.id.et_code);
        btn_login = (Button) findViewById(R.id.btn_login);
        timeButton = (TimeButton) findViewById(R.id.time_button);
        btn_login.setOnClickListener(this);
        timeButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.time_button:
                presenter.verifyCanLogin(et_phone.getText().toString().trim());
                break;
            case R.id.btn_login:
                presenter.toLogin(et_phone.getText().toString().trim(), et_code.getText().toString().trim());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timeButton.onDestroy();
    }

    @Override
    protected LoginPresenter providePresenter() {
        return presenter = new LoginPresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    @Override
    public void onError(String msg) {
        hideLoading();
        showErrorMsg(msg);
    }

    @Override
    public void onLoading() {
        showLoading();
    }

    @Override
    public void onSuccess() {
        hideLoading();
        presenter.getVerifyCode(et_phone.getText().toString().trim());
    }

    @Override
    public void onGetSuccess(CodeBean codeBean) {
        hideLoading();
        timeButton.setTimer();
        showErrorMsg("短信已发送，请注意查收");
    }

    @Override
    public void onLoginSuccess() {
        hideLoading();
        Intent intent = new Intent(LoginActivity.this, SelectCompanyActivity.class);
        startActivity(intent);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ModuleManager.newInstance().setUHFStatus(false);
            MyApplication.getInstance().onTerminate();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ModuleManager.newInstance().setUHFStatus(false);
        MyApplication.getInstance().onTerminate();
    }
}
